//
//  OKBtnScroll.m
//  shoppingCat
//
//  Created by OKboxun--weixin on 2017/2/27.
//  Copyright © 2017年 OKboxun--weixin. All rights reserved.
//

#import "OKBtnScroll.h"

@interface OKBtnScroll ()

@property (nonatomic,strong)UIView *SliderView ;//红色滑块

@end

@implementation OKBtnScroll

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _arrM = [NSMutableArray array];
        self.bounces = NO;
        self.showsHorizontalScrollIndicator = NO;
    }
    return self;
}

-(void)setArrM:(NSMutableArray *)arrM
{
    _arrM = arrM;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.SliderView.frame = CGRectMake(12.5, 32, textSizeWidth(@"推荐",10000, 16,[UIFont systemFontOfSize:16]), 2);
    }] ;
    self.contentOffset = CGPointMake(0, 0);

    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIButton *lastBtn = nil;
    
    for (int i = 0; i<arrM.count; i++) {
        
        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];

        button.tag = 100+i;
        
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        
        [button setTitle:arrM[i] forState:UIControlStateNormal];
        
        [button setTitle:arrM[i] forState:UIControlStateDisabled];
        
        [button setImage:[UIImage imageNamed:@"搜索条件_更多"]  forState:UIControlStateNormal];
        
        [button setImage:[[UIImage imageNamed:@"搜索条件_更少"] imageWithColor:baseColorTheme] forState:UIControlStateDisabled];

        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [button setTitleColor:baseColorTheme forState:UIControlStateDisabled];
//        button.backgroundColor = RandomColor;
        
        CGFloat width = [self widthBtnHeight:self.frame.size.height font:[UIFont systemFontOfSize:16] text:arrM[i]]+40;
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [button sizeToFit];
        button.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
        button.imageEdgeInsets = UIEdgeInsetsMake(0, width-40, 0, -width+40);
        
        if (lastBtn == nil) {
            button.frame = CGRectMake(0,0, width, 40);
            button.enabled = NO;
            self.lastButton = button;
        }else{
            button.frame = CGRectMake(CGRectGetMaxX(lastBtn.frame),0, width, 40);
        }
        
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:button];
        
        lastBtn = button;
        
    }
    self.contentSize = CGSizeMake(CGRectGetMaxX(lastBtn.frame), 40);
    [self addSubview:self.SliderView];
}

-(void)btnClick:(UIButton*)btn
{
    [self changeBtn:btn.tag];
    if (_block) {
        self.block(btn.tag);
    }
    
}

-(void)changeBtn:(NSInteger)index{
        //imageWithColor 搜索条件_更多
    UIButton *btn = [self viewWithTag:index];
    self.lastButton.enabled = YES;
    btn.enabled = NO;
    
    __weak __typeof(self)weakSelf = self;
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = btn.frame;
        frame.origin.x=frame.origin.x+7.5;
        frame.origin.y=32;
        frame.size.height=2;
        frame.size.width=textSizeWidth(btn.titleLabel.text,10000, 16,[UIFont systemFontOfSize:16])+10;
        weakSelf.SliderView.frame =frame;
        weakSelf.SliderView.center = CGPointMake(btn.center.x-1,weakSelf.SliderView.center.y);
    }] ;
    
    //设置图标超出屏幕滚动到中间
    CGRect rect = btn.frame;
    rect.size.width += self.frame.size.width-30;
    rect.origin.x -= self.frame.size.width/2.0+20;
    [self scrollRectToVisible:rect animated:YES];
    
    self.lastButton = btn;
    
}

-(CGFloat)widthBtnHeight:(CGFloat)height font:(UIFont*)font text:(NSString*)str
{
    return  [str boundingRectWithSize:CGSizeMake(10000,height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size.width;
}

-(UIView *)SliderView
{
    if (!_SliderView) {
        _SliderView = [[UIView alloc]initWithFrame:CGRectMake(12.5, 32, textSizeWidth(@"推荐",10000, 16,[UIFont systemFontOfSize:16]), 2)];
        _SliderView.backgroundColor = baseColorTheme;
    
    
    }
    return _SliderView;
}
@end
